package com.qfmy.app.service.impl.community;

import com.qfmy.app.mapper.community.BlogClassMapper;
import com.qfmy.app.service.community.BlogClassService;
import com.qfmy.common.result.Result;
import com.qfmy.model.entity.community.BlogClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 清风明月
 * @Package com.qfmy.app.service.impl.community
 * @date 2025/4/17
 * @description 博客分类
 */
@Service
@SuppressWarnings("all")
public class BlogClassServiceImpl implements BlogClassService {

    /**
     * 注入mapper
     */
    @Autowired
    private BlogClassMapper blogClassMapper;
    /**
     * 注入redisTemplate
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取热门分类
     * @return
     */
    @Override
    public List<BlogClass> getHotClass() {
        //创建key
        String key = "hot_blog_class";
        //从redis中获取数据
        List<BlogClass> blogClassList = (List<BlogClass>) redisTemplate.opsForValue().get(key);
        //判断是否为空
        if (blogClassList != null) {
            return blogClassList;
        }
        //从数据库中获取数据
        blogClassList = blogClassMapper.getHotClass();
        //存入redis
        redisTemplate.opsForValue().set(key, blogClassList,1, TimeUnit.HOURS);
        return blogClassList;
    }

    /**
     * 获取所有分类
     * @return
     */
    @Override
    public List<String> getAllClass() {
        List<String> blogClassList = blogClassMapper.getAllClass();
        return blogClassList;
    }

    /**
     * 根据分类获取博客
     *
     * @param className
     * @return
     */
    @Override
    public List<BlogClass> getByClass(String className) {
        List<BlogClass> blogClassList = blogClassMapper.getByClass(className);
        return blogClassList;
    }

    /**
     * 更新类别的浏览次数
     * @param className
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClass(String className) {
        blogClassMapper.updateClass(className);
    }
}
